const { ccclass, property } = cc._decorator;
import MonsterControl from './MonsterControl';
import MonstersBilder from './MonstersBilder';
import WayBaseControl from '../World/WayBaseControl';

@ccclass
export default class MonsterPortal extends cc.Component {
    @property(cc.Node)
    MonsterNode: cc.Node = null;
    PortalJson: any = null;
    monstersBilder: MonstersBilder = null;
    _Anim: cc.Animation = null;
    _element: cc.Node = null;
    _TheNumbered: number = 0;
    _Number: number = 0
    _isBild: boolean = false;
    onLoad() {
        this._Anim = this.node.getComponent(cc.Animation);
        this._Anim.on(cc.Animation.EventType.FINISHED, this.onEndAnim, this);
        let schedule = this.PortalJson.Schedule;
        if (schedule) {
            this._Number = schedule.TheNumber;
            this.schedule(this.playbild, schedule.Interval, schedule.Repeat - 1, schedule.StarTime);
        }
        if (schedule?.HowLong > 0) {
            this.scheduleOnce(function () {
                this.unschedule(() => { this.playbild() });
                this.node.removeFromParent();
                this.node.destroy();
            }, schedule.HowLong + schedule.StarTime);
        }
    }
    onEndAnim() {
        let name = this._Anim.currentClip.name
        if (name == 'PortaBegin') {
            this._Anim.play('Portaling');
            this.bild();
            this._isBild = true;
        }
        else if (name == 'Portaling') {
            if (this._element) {
                this._element.setParent(this.MonsterNode);
                this._element = null;
            }

            if (this._TheNumbered < this._Number) {
                this.moveAnim();
                this._isBild = true;
            }
            else {
                this._TheNumbered = 0;
                this._Anim.play('PortaEnd');
                this._isBild = false;
            }
        }
    }
    private playbild() {
        if (this.monstersBilder.MonsterControl >= this.PortalJson.AllMax) {
            return;
        }
        let way = this.monstersBilder.getCheckWay(this.PortalJson.CreateType);
        if (way) {
            let control = way.getComponent(WayBaseControl);
            let index = control.getEmptyIndex(100);
            if (index != -1) {
                let Px = 0;
                if (control) {
                    Px = control.getItemNodeX(index);
                }
                this.node.x = Px;
                this._Anim.play('PortaBegin');
                this.node.parent = way;
                this.node.y = control.floorY + this.PortalJson.PortalY;
            }
        }
    }
    private moveAnim() {
        if (this.node.x > 0) {
            cc.tween(this.node)
                .by(0.5, { x: -60 })
                .call(() => {
                    this.bild();
                    this._Anim.play('Portaling');
                })
                .start()
        }
        else {
            cc.tween(this.node)
                .by(0.5, { x: 60 })
                .call(() => {
                    this.bild();
                    this._Anim.play('Portaling');
                })
                .start()
        }
    }
    private bild() {
        this._element = this.monstersBilder.getMonster();
        if (this._element) {
            this._element.active = true;
            this._element.setPosition(new cc.Vec2(0, 0));
            this._element.zIndex = 99;
            this._element.parent = this.node;
            this._element.getComponent(MonsterControl).birth()
            this._TheNumbered++;
        }
    }
}
